package com.sensor;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

public class Test extends Activity {
    /** Called when the activity is first created. */
	
	private SensorManager sensorManager = null;
	private Sensor acceSensor = null;
	
	private Handler handler = null;
	
	private TextView x = null;
	private TextView y = null;
	private TextView z = null;
	
	private SensorEventListener mySensorEventListener = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        x = (TextView) this.findViewById(R.id.x);
        y = (TextView) this.findViewById(R.id.y);
        z = (TextView) this.findViewById(R.id.z);
        
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        
        acceSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        
//        handlerThread = new HandlerThread("thread-opp");
//        handlerThread.start();
        
        System.out.println(Looper.myLooper().getThread().getId());
        System.out.println(Looper.getMainLooper().getThread().getId());
        
        handler = new Handler(Looper.myLooper()){
        	public void handleMessage(android.os.Message msg) {
        		SensorEvent event = (SensorEvent) msg.obj;
//        		System.out.println("event.values[x]:"+event.values[0]);
//        		System.out.println("event.values[y]:"+event.values[1]);
//        		System.out.println("event.values[z]:"+event.values[2]);
        		x.setText("event.values[x]:"+event.values[0]);
    			y.setText("event.values[y]:"+event.values[1]);
    			z.setText("event.values[z]:"+event.values[2]);
    			
    			Double a = Double.valueOf(event.values[0]);
    			Double c = Double.valueOf(event.values[1]);
    			
    			double acos = Math.acos(Math.abs(a)/Math.sqrt((Math.pow(a, 2)+Math.pow(c, 2))));
    			
        	};
        };
        
        mySensorEventListener = new MySensorEventListener(handler);
    }
    
    
    class MySensorEventListener implements SensorEventListener{
    	private Message msg = null;
    	private Handler handler = null;
    	
    	public MySensorEventListener(Handler handler){
    		this.handler = handler;
    	}

		public void onSensorChanged(SensorEvent event) {
			msg = handler.obtainMessage();
			msg.obj = event;
			handler.sendMessage(msg);
		}

		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			
		}
    	
    }
    
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
    	//currently we create one Handler instance per listener, but we could have one per looper
    	//针对多个传感器相互协作的时候使用
    	sensorManager.registerListener(mySensorEventListener, acceSensor, SensorManager.SENSOR_DELAY_NORMAL, handler);
    }
    
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	
    	sensorManager.unregisterListener(mySensorEventListener, acceSensor);
    }
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    }
}


